package domain

import (
	"gluenet/internal/glue-srvp/domain/aggregate"
	"gluenet/internal/glue-srvp/domain/entity"
	"gluenet/internal/glue-srvp/domain/vo"
	"gluenet/internal/pkg/infra"
	"gluenet/internal/pkg/infra/prepare"
)

// 创建对象，并把对象放入依赖池，架构自动构建依赖
func init() {
	//fmt.Printf("domina init\n")
	prepare.RegisterService(func() {
		config := NewConfigDomain()
		prepare.InjectDependency(func() *Config {
			return config
		})
	})
}

// Config 应用配置领域，管理应用配置的服务在这里
type Config struct {
	ConfigFactory *aggregate.ConfigFactory
	TagFactory    *aggregate.TagFactory
}

// Create 创建配置
func (r *Config) Create(auth infra.AuthInfo, req vo.CreateConfigReq) (interface{}, error) {
	cfg, err := r.ConfigFactory.AddConfig(auth, req)
	if err != nil {
		return nil, err
	}
	//cfgRes := cfg.ShowInfo()

	return cfg, nil
}

// Delete 删除
func (r *Config) Update(auth infra.AuthInfo, req vo.UpdateConfigReq) (*entity.Config, error) {
	return r.ConfigFactory.UptConfig(auth, req)
}

// Delete 删除
func (r *Config) Delete(auth infra.AuthInfo, id int64) (*entity.Config, error) {
	return r.ConfigFactory.DelConfig(auth, id)
}

// FindByUser 查询配置
func (r *Config) FindBy(auth infra.AuthInfo, query map[string]interface{}) ([]entity.Config, error) {
	return r.ConfigFactory.Findby(auth, query)
}

// FindByUser 查询配置
func (r *Config) Table() string {
	var t entity.Config
	return t.TableName()
}

func NewConfigDomain() *Config {
	domain := &Config{}
	prepare.FetchDependency(&domain.ConfigFactory)
	return domain
}
